package com.sanjiahantang.youjiaclient.module.common.views.impl;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpFragment;
import com.sanjiahantang.youjiaclient.module.common.adapters.ShopAdapter;
import com.sanjiahantang.youjiaclient.module.common.presenters.ShopPresenter;
import com.sanjiahantang.youjiaclient.module.common.views.ShopView;
import com.zbar.lib.CaptureActivity;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TYPE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.SEARCH_TYPE_GOODS;


/**
 * Created by zy on 2017/9/5.
 * 商城
 */

public class ShopFragment extends BaseMvpFragment<ShopView,ShopPresenter>
        implements ShopView,View.OnClickListener,SwipeRefreshLayout.OnRefreshListener {

    private View rootView,rlCategory,vsNetworkError;
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout refreshLayout;
    private LinearLayout search;
    private LinearLayoutManager layoutManager;
    private boolean isFreshing;




    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_shopping, container, false);
        }

        return rootView;
    }

    @Override
    protected void initView() {
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        search = (LinearLayout) rootView.findViewById(R.id.llSearchBar);
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refreshLayout);
        rlCategory = rootView.findViewById(R.id.rlCategory);
        search.setOnClickListener(this);
        layoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(layoutManager);
        refreshLayout.setOnRefreshListener(this);
        rlCategory.setOnClickListener(this);

        vsNetworkError = rootView.findViewById(R.id.vsNetworkError);
    }

    @Override
    protected void initData() {
        if (Utils.NO_NETWORK_STATE == Utils.isNetworkAvailable(context)) {
            onNetError(true);
            return;
        }else{
            onNetError(false);
        }
        presenter.getShopData(context);
    }

    private void onNetError(boolean b) {
        if (b){
            refreshLayout.setVisibility(View.GONE);
            vsNetworkError.setVisibility(View.VISIBLE);
            vsNetworkError.findViewById(R.id.btnReloadNetwork).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initData();
                }
            });
        }else{
            refreshLayout.setVisibility(View.VISIBLE);
            vsNetworkError.setVisibility(View.GONE);
        }
    }


    @Override
    public ShopPresenter initPresenter() {
        return new ShopPresenter();
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.llSearchBar:
                Intent search = new Intent(context, SearchActivity.class);
                search.putExtra(KEY_TYPE,SEARCH_TYPE_GOODS);
                startActivity(search);
                break;
            case R.id.rlCategory:
                presenter.goPageCategory(context);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent itCapture = new Intent(getActivity(), CaptureActivity.class);
                startActivity(itCapture);

            } else {
                Toast.makeText(context, "权限被禁止，相关功能无法使用！", Toast.LENGTH_SHORT).show();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }


    @Override
    public void showProgress() {
        refreshLayout.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        if (isFreshing) {
            return;
        } else {
            refreshLayout.setRefreshing(true);
            isFreshing = true;
            initData();
        }
    }

    @Override
    public void shopRefresh(ShopAdapter adapter) {
        refreshLayout.setRefreshing(false);
        isFreshing = false;
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
    }
}
